package org.mushroomdb.parser.sql92.impl.util;

public interface Trim {
	static final Trim NONE = new NoneTrim();
	static final Trim LEADING = new LeadingTrim();
	static final Trim TRAILING = new TrailingTrim();
	static final Trim BOTH = new BothTrim();
	
	String trim(String sample, char trimChar);
}

class NoneTrim implements Trim {

	public String trim(String sample, char trimChar) {
		return sample;
	}
	
}

class LeadingTrim implements Trim {

	public String trim(String sample, char trimChar) {
		String aChar = Character.toString(trimChar);
		while (sample.startsWith(aChar)) sample = sample.substring(1);
		return sample;
	}
	
}

class TrailingTrim implements Trim {

	public String trim(String sample, char trimChar) {
		String aChar = Character.toString(trimChar);
		while (sample.endsWith(aChar)) sample = sample.substring(0, sample.length() - 2);
		return sample;
	}
	
}

class BothTrim implements Trim {

	public String trim(String sample, char trimChar) {
		return Trim.TRAILING.trim(
				Trim.LEADING.trim(sample, trimChar)
				, trimChar
				);
	}
	
}
